package com.zego.wawaji.ui.center.address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.bean.City;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.utils.ChineseFilter;
import com.zego.wawaji.base.utils.UIUtil;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.center.address.bean.Address;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.dialog.address.AddressPicker;

/**
 * 我的地址：编辑
 * Created by Su on 2017/12/1.
 */
public class MyEditAddressAct extends BaseAppCompatActivity implements View.OnClickListener {
    private EditText eit_name, eit_phone, eit_detail;
    private TextView tvAddress;

    private String name, phone, provinceName, cityName, address, addr_detail;

    private Address data;      // 上个页面传回数据
    private boolean isAdd = true;  // 区分是修改还是添加

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_address_edit_act);
        initTitle();
        initView();
    }

    private void initView() {
        data = getIntent().getParcelableExtra("Address");
        if (data != null) isAdd = false;

        eit_name = (EditText) findViewById(R.id.eit_name);
        eit_phone = (EditText) findViewById(R.id.eit_phone);
        eit_detail = (EditText) findViewById(R.id.eit_detail);
        tvAddress = (TextView) findViewById(R.id.address);
        findViewById(R.id.address_view).setOnClickListener(this);

        if (isAdd) return;

        eit_name.setText(data.getReceiverName());
        eit_phone.setText(data.getTelephone());
        eit_detail.setText(data.getStreet());
        address = data.getProvince() + " " + data.getCity();
        tvAddress.setText(address);
        UIUtil.endCursor(eit_name);
    }

    private void initTitle() {
        setTitle("新增收货地址");
        setBackView();
        setTitleRight("保存", this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_right:
                if (check()) {
                    if (isAdd) {
                        ModuleMgr.getCenterMgr().reqAddAddress(name, phone, provinceName, cityName, addr_detail, new RequestComplete() {
                            @Override
                            public void onRequestComplete(HttpResponse response) {
                                if (response.isOk())
                                    finish();
                            }
                        });
                        return;
                    }
                    ModuleMgr.getCenterMgr().reqUpdataAddress(data.getId(), name, phone, provinceName, cityName, addr_detail, new RequestComplete() {
                        @Override
                        public void onRequestComplete(HttpResponse response) {
                            if (response.isOk())
                                finish();
                        }
                    });
                }
                break;

            case R.id.address_view:
                PickerDialogUtil.showAddressPickerDialog2(this, new AddressPicker.OnAddressPickListener() {
                    @Override
                    public void onAddressPicked(City city) {
                        provinceName = city.getProvinceName();
                        cityName = city.getCityName();
                        address = provinceName + " " + cityName;
                        tvAddress.setText(address);
                    }
                }, provinceName, cityName);
                break;
        }
    }

    private boolean check() {
        name = eit_name.getText().toString();
        if (TextUtils.isEmpty(name)) {
            PToast.showShort("请输入收货人姓名");
            return false;
        }

        phone = eit_phone.getText().toString();

        if (TextUtils.isEmpty(phone) || !ChineseFilter.isPhoneNumber(phone)) {
            PToast.showShort("请输入正确格式手机号");
            return false;
        }

        if (TextUtils.isEmpty(address)) {
            PToast.showShort("请输入地址");
            return false;
        }

        addr_detail = eit_detail.getText().toString();
        if (TextUtils.isEmpty(addr_detail)) {
            PToast.showShort("请输入详细地址");
            return false;
        }
        return true;
    }
}
